package com.tsingteng.cosfun.ui.login;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.app.App;
import com.tsingteng.cosfun.app.AppContext;
import com.tsingteng.cosfun.base.BasePresenterActivity;
import com.tsingteng.cosfun.bean.CommBackBean;
import com.tsingteng.cosfun.bean.ReportBean;
import com.tsingteng.cosfun.callback.AppListener;
import com.tsingteng.cosfun.callback.PermissionInterface;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.crashUtils.utils.ExceptionUpLoader;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.ui.login.report.ReportContract;
import com.tsingteng.cosfun.ui.login.report.ReportPresenter;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.PermissionHelper;
import com.tsingteng.cosfun.utils.SPUtils;
import com.tsingteng.cosfun.utils.TimeUtils;
import com.tsingteng.cosfun.utils.ToastUtils;
import com.tsingteng.cosfun.utils.VideoUtils;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/9/6
 * qq(872179966)
 */

public class SplashActivity extends BasePresenterActivity<ReportPresenter, ReportContract.IReportView> implements PermissionInterface, ReportContract.IReportView {
    private PermissionHelper mPermissionHelper;
    private Handler handler = new Handler();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);


    }

    @Override
    public int getPermissionsRequestCode() {
        //设置权限请求requestCode，只有不跟onRequestPermissionsResult方法中的其他请求码冲突即可。
        return 10000;
    }

    @Override
    public String[] getPermissions() {
        //设置该界面所需的全部权限
        return new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
        };
    }

    @Override
    public void requestPermissionsSuccess() {
        handler.postDelayed(() -> {
            Navigate.startMain(SplashActivity.this,false);
            finish();
        }, 2000);
        boolean isLogin = LogonUtils.getIsLogin();
        int iLogin = SPUtils.init().getInt("isLoginCount");
        if(iLogin == 2 && !isLogin){
            SPUtils.init().putInt("isLoginCount",1);
            AppContext.init();
            mPresenter.loginReport();
        }
        mPresenter.getNotReadCount();
        if (LogonUtils.getIsLogin()) {
            if (AppUtils.getFollowTime()==-1) {
                mPresenter.getFollowCount(0);
            }else {
                mPresenter.getFollowCount(AppUtils.getFollowTime());
            }
        }

        if (!TextUtils.isEmpty(AppUtils.isHaveException())) {
            ExceptionUpLoader.getmInstance().upLoadCrashLog(new ExceptionUpLoader.ExceptionUpLoadListener() {
                @Override
                public void exceptionSuccess() {
                }
                @Override
                public void exceptionFail() {
                }
            });
        }
    }

    @Override
    public void requestPermissionsFail() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (mPermissionHelper.requestPermissionsResult(requestCode, permissions, grantResults)) {
            AppUtils.setGid();
            AppContext.init();
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void showReportData(ReportBean data) {

    }

    @Override
    public void showReadCount(ReportBean data) {
        VideoHelper.getIntence().setNotRead(data.getNotReadCount());
    }

    @Override
    public void showReadAll() {

    }

    @Override
    public void showFollowCount(CommBackBean commBackBean) {
        AppUtils.setFollowTime(System.currentTimeMillis()/1000);
        AppUtils.setFollowCount(commBackBean.getCount());

    }

    @Override
    protected void initData() {
        //初始化并发起权限申请
        mPermissionHelper = new PermissionHelper(this, this);
        mPermissionHelper.requestPermissions();
    }

    @Override
    protected ReportPresenter createPresenter() {
        return new ReportPresenter();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_splash;
    }

    @Override
    protected void getIntentData(Intent intent) {

    }
}
